home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Development Kits / Mac OS / USB DDK 1.4.6f4 / Examples / USBModem / ModemStub.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-26  |  8.4 KB  |  296 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ModemStub.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1998-2000 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include <Errors.h>
  13.  
  14. #include "Modem.h"
  15. #include "ModemStub.h"
  16. #include "ModemDriver.h"
  17. #include "ShimSerialStub.h"
  18. #include "ShimSerialHAL.h"
  19. #include "ModemVersion.h"
  20.  
  21. //------------------------------------------------------
  22. //
  23. // Globals
  24. //
  25. //------------------------------------------------------
  26.  
  27. ShimSerialGlobals*    gGlobals;
  28.  
  29. //------------------------------------------------------
  30. //
  31. //    This is the driver description structure that the expert looks for first.
  32. //  If it's here, the information within is used to match the driver
  33. //  to the device whose descriptor was passed to the expert.
  34. //    Information in this block is also used by the expert when an
  35. //  entry is created in the Name Registry.
  36. //
  37. //------------------------------------------------------
  38.  
  39. USBDriverDescription    TheUSBDriverDescription = 
  40. {
  41.     // Signature info
  42.     kTheUSBDriverDescriptionSignature,
  43.     kInitialUSBDriverDescriptor,
  44.     
  45.     // Device Info
  46.     kUSBVendor,                             // vendor ID
  47.     kUSBProduct,                            // product ID
  48.     0,                                        // version of product = not device specific
  49.     0,                                        // protocol = not device specific
  50.     
  51.     // Interface Info            
  52.     0,                                        // Configuration Value (doesn't matter)
  53.     0,                                        // Interface Number
  54.     kUSBCommClass,                            // Interface Class
  55.     2,                                         // Interface SubClass (Abstract Control Model)
  56.     1,                                        // Interface Protocol (V.25ter)    
  57.     
  58.     // Driver Info
  59.     kModemName,                                // Driver name for Name Registry
  60.     kUSBCommClass,                            // Device Class
  61.     0,                                        // Device Subclass 
  62.     kVMajor,                                 // version of driver (see ModemVersion.h)
  63.     kVMinor, 
  64.     kVStage, 
  65.     kVRelease,
  66.     
  67.     // Driver Loading Info
  68. //    kUSBDoNotMatchGenericDevice                // Flags
  69.     0
  70. };
  71.     
  72. USBClassDriverPluginDispatchTable TheClassDriverPluginDispatchTable =
  73. {
  74.     kClassDriverPluginVersion,                // Version of this structure
  75.     modemDriverValidateHW,                    // Hardware Validation Procedure
  76.     modemDriverInitialize,                    // Initialization Procedure
  77.     modemDriverInitInterface,                // Interface Initialization Procedure
  78.     modemDriverFinalize,                    // Finalization Procedure
  79.     modemDriverNotifyProc,
  80. };
  81.  
  82. CFragConnectionID    ConnID;                    // Need to remember this
  83.  
  84. /***********************************************************************************/
  85. //    Function:        modemDriverValidateHW(USBDeviceRef device, USBDeviceDescriptor *desc)
  86. //    Description:    Called upon load by Expert
  87. //
  88. //    Input:            Device reference, Device descriptor
  89. //    Output:            noErr
  90. /***********************************************************************************/
  91.  
  92. static OSStatus modemDriverValidateHW(USBDeviceRef device, USBDeviceDescriptor *desc)
  93. {
  94. #pragma unused (device, desc)
  95.  
  96.     TraceMessage(0,kCRMName"- Entering modemDriverValidateHW");
  97.         
  98.     return noErr;
  99. }
  100.  
  101. /***********************************************************************************/
  102. //    Function:        OSStatus modemDriverInitialize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc,
  103. //                                                                         UInt32 busPowerAvailable)
  104. //    Description:    Called upon load by Expert
  105. //
  106. //    Input:            Device reference, Device descriptor, bus power available
  107. //    Output:            result
  108. /***********************************************************************************/
  109.  
  110. static OSStatus modemDriverInitialize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc, UInt32 busPowerAvailable)
  111. {
  112. #pragma unused (busPowerAvailable)
  113.     
  114. OSStatus err;
  115.  
  116.     TraceMessage(0,kCRMName"- Entering modemDriverInitialize");
  117.  
  118.     gGlobals = (ShimSerialGlobals*)PoolAllocateResident(sizeof(ShimSerialGlobals),true);
  119.     if (gGlobals == NULL)
  120.         goto bail;
  121.     
  122.     USBExpertStatus(0, kCRMName"- Modem driver loading as Class - "kVers2Long Bugon, 0);
  123.     
  124.     err = modemDriverEntry(device, pDesc, busPowerAvailable);
  125.  
  126.     if (err != noErr)
  127.     {
  128.         StatusMessage(0, kCRMName"- Configuration failed", err);
  129.         goto bail;
  130.     } else {
  131.         //    Install Device Control Entry for both the ".In" and ".Out functions that are
  132.         //    requied for a serial driver which is what we are.
  133.         if (noErr != InstallShimDrvr(ConnID))
  134.         {
  135.             StatusMessage(0, kCRMName" - Can't install DCEs!", 0);
  136.             goto bail;
  137.         }
  138.     }
  139.  
  140.     return noErr;
  141.  
  142. bail:
  143.     if (gGlobals)
  144.         PoolDeallocate(gGlobals);
  145.     gGlobals = NULL;
  146.     
  147.     return openErr;
  148. }
  149.  
  150. /***********************************************************************************/
  151. //    Function:        modemDriverInitInterface(UInt32 interfaceNum, USBInterfaceDescriptor *interfaceDesc, 
  152. //                                                    USBDeviceDescriptor    *deviceDesc, USBDeviceRef device)
  153. //    Description:    Called to initialize driver for an individual interface - 
  154. //                    either by expert or internally by driver
  155. //
  156. //    Input:            Interface number, Interface descriptor, Device Descriptor, Device Reference
  157. //    Output:            result
  158. /***********************************************************************************/
  159.  
  160. static OSStatus modemDriverInitInterface(UInt32 interfaceNum, USBInterfaceDescriptor *interfaceDesc, USBDeviceDescriptor *deviceDesc, USBDeviceRef device)
  161. {
  162. #pragma unused (interfaceNum, interfaceDesc)
  163.  
  164.     OSStatus err;
  165.  
  166.     TraceMessage(0,kCRMName"- Entering modemDriverInitInterface");
  167.  
  168.     if (gGlobals == NULL)
  169.         gGlobals = (ShimSerialGlobals*)PoolAllocateResident(sizeof(ShimSerialGlobals),true);
  170.  
  171.     if (gGlobals == NULL)
  172.         goto bail;
  173.  
  174.     USBExpertStatus(0, kCRMName"- Modem driver loading as Interface "kVers2Long Bugon, 0);
  175.     
  176.     err = modemDriverEntry(device, deviceDesc, 0);
  177.  
  178.     if (err != noErr)
  179.     {
  180.         StatusMessage(0, kCRMName"- Configuration failed", err);
  181.         goto bail;
  182.     } else {
  183.         //    Install Device Control Entry for both the ".In" and ".Out functions that are
  184.         //    requied for a serial driver which is what we are.
  185.         if (noErr != InstallShimDrvr(ConnID))
  186.         {
  187.             StatusMessage(0, kCRMName"- Can't install DCEs!", 0);
  188.             goto bail;
  189.         }
  190.     }
  191.         
  192.     return noErr;
  193.  
  194. bail:
  195.     if (gGlobals)
  196.         PoolDeallocate(gGlobals);
  197.     gGlobals = NULL;
  198.     
  199.     return openErr;
  200. }
  201.  
  202. /***********************************************************************************/
  203. //    Function:        modemDriverFinalize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc)
  204. //    Description:    Called by Expert when driver is being shut down
  205. //
  206. //    Input:            Device reference, Device descriptor
  207. //    Output:            noErr
  208. /***********************************************************************************/
  209.  
  210. static OSStatus modemDriverFinalize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc)
  211. {
  212. #pragma unused (device, pDesc)
  213.  
  214.     TraceMessage(0,kCRMName"- Entering modemDriverFinalize");
  215.     
  216.     if (gGlobals)
  217.     {
  218.         /* This is done here just as a safeguard because it should have been done in the notify proc */    
  219. //        KillUSBIO();
  220.         ResetDevice();
  221.         RemoveShimDrvr(true);                            // force close - let the shim handle it
  222.         PoolDeallocate(gGlobals);
  223.         gGlobals = NULL;
  224.     }
  225.     
  226.     return noErr;
  227. }
  228.  
  229. /***********************************************************************************/
  230. //    Function:        ExpertEntryProc(ExpertNotificationProcPtr pExpertNotify)
  231. //    Description:    
  232. //
  233. //    Input:            Notification proc pointer
  234. //    Output:            noErr
  235. /***********************************************************************************/
  236.  
  237. static OSStatus ExpertEntryProc(ExpertNotificationProcPtr pExpertNotify)
  238. {
  239. #pragma unused (pExpertNotify)
  240.  
  241.     TraceMessage(0,kCRMName"- Entering ExpertEntryProc");
  242.  
  243.     return noErr;
  244. }
  245.  
  246. /***********************************************************************************/
  247. //    Function:        modemDriverNotifyProc(UInt32 notification, void *pointer)
  248. //    Description:    
  249. //
  250. //    Input:            Notification and pointer
  251. //    Output:            result
  252. /***********************************************************************************/
  253.  
  254. static OSStatus    modemDriverNotifyProc(UInt32 notification, void *pointer, UInt32 refCon)
  255. {
  256. #pragma unused (pointer, refCon)
  257.     
  258.     TraceMessage(0,kCRMName"- Entering modemDriverNotifyProc");
  259.     StatusMessage(0, kCRMName"- Driver Notification = ", notification);
  260.     
  261.     if (notification == kNotifyRemoveDevice)
  262.     {
  263.         if (gGlobals->ShimRef != kInvalidRef)
  264.         {
  265.             KillUSBIO();
  266.             RemoveShimDrvr(true);                            // force close - let the shim handle it
  267.  
  268.             if (gGlobals)
  269.             {
  270.                 PoolDeallocate(gGlobals);
  271.                 gGlobals = NULL;
  272.             } 
  273.         }
  274.     }
  275.     
  276.     return noErr;
  277. }
  278.  
  279. /***********************************************************************************/
  280. //    Function:        InitDriver(CFragInitBlock *init)
  281. //    Description:    This routine saves our connection id
  282. //
  283. //    Input:            init (unused)
  284. //    Output:            noErr
  285. /***********************************************************************************/
  286.  
  287. OSErr InitDriver(CFragInitBlock *init)
  288. {
  289.     OSErr    err = noErr;
  290.     
  291.     TraceMessage(0, kCRMName"- Entering initDriver");
  292.     
  293.     ConnID = init->connectionID;
  294.  
  295.     return err;
  296. }